/**
 * Created by Administrator on 2018/5/10.
 */
define(function (require,exports,module) {
    require("./gante.css");
    require("./gante.css");
    require("../../../lib/gojs/go-debug.js")
    require("../../../lib/fullcalendar/moment.min.js");
    var sTpl = require("./template.html");
    var VueComponent = Vue.extend({
        template: sTpl,
        data:function () {
            var form={
                startDate:'',
                endDate:'',
                flightNum:'',
                flightInfoId:''
            }
            return {
               form:form,
                myDiagram:{},
                loadJs:false
            };
        },
        mounted:function () {
            this.findDuty();
        }
        ,
        methods:{
            findDuty:function () {
               axios.get(basepath+"/api/duty/quey?startDate="+moment().format("YYYY-MM-DD")+
                   "&endDate="+moment().format("YYYY-MM-DD")+"").then(function (response) {
                       var tree = {};
                   var b=response.data.map(function(data){
                       var a={};
                       //{"key":2, "name":"李四", "title":"VP Marketing/Sales", "parent":1},
                       if(data[0].parentCode){
                           a.parent = data[0].parentCode;
                       }
                       a.key =data[0].teamId;
                       a.title = data[0].teamName;
                       if(data[1]){
                           a.name =data[1].userCnName;

                       }else{
                           a.name="";
                       }
                       tree[a.key] = a;
                       return a;

                   })
                   b.forEach(function (data) {
                       if(data.name){
                           var t=tree[data.parent];
                           if(t){
                               t.hasChildren=true;
                               data.hasChildren = true;
                               while(t.parent){
                                   t=tree[t.parent];
                                   t.hasChildren= true;
                               }
                           }

                       }
                   });
                   b=b.filter(function (d) {
                       if(d.hasChildren)return d;
                   })
                   myDiagram.model=go.Model.fromJson({ "class": "go.TreeModel",
                       "nodeDataArray": b
                   });

                   //console.log(response.data);

               })
            },
            draw:function () {
                // init for these samples -- you don't need to call this
                var goMake = go.GraphObject.make;  // for conciseness in defining templates

                this.myDiagram =
                    goMake(go.Diagram, "myDiagramDiv", // must be the ID or reference to div
                        {
                            initialContentAlignment: go.Spot.Center,
                            maxSelectionCount: 1, // users can select only one part at a time
                            validCycle: go.Diagram.CycleDestinationTree, // make sure users can only create trees
                            "clickCreatingTool.archetypeNodeData": {}, // allow double-click in background to create a new node
                            "clickCreatingTool.insertPart": function(loc) {  // customize the data for the new node
                                // this.archetypeNodeData = {
                                //     key: getNextKey(), // assign the key based on the number of nodes
                                //     name: "(new person)",
                                //     title: ""
                                // };
                                // return go.ClickCreatingTool.prototype.insertPart.call(this, loc);
                            },
                            layout:
                                goMake(go.TreeLayout,
                                    {
                                        treeStyle: go.TreeLayout.StyleLastParents,
                                        arrangement: go.TreeLayout.ArrangementHorizontal,
                                        // properties for most of the tree:
                                        angle: 90,
                                        layerSpacing: 35,
                                        // properties for the "last parents":
                                        alternateAngle: 90,
                                        alternateLayerSpacing: 35,
                                        alternateAlignment: go.TreeLayout.AlignmentBus,
                                        alternateNodeSpacing: 20
                                    }),
                            "undoManager.isEnabled": true // enable undo & redo
                        });

                myDiagram = this.myDiagram;
                // when the document is modified, add a "*" to the title and enable the "Save" button
                myDiagram.addDiagramListener("Modified", function(e) {
                    var button = document.getElementById("SaveButton");
                    if (button) button.disabled = !myDiagram.isModified;
                    var idx = document.title.indexOf("*");
                    if (myDiagram.isModified) {
                        if (idx < 0) document.title += "*";
                    } else {
                        if (idx >= 0) document.title = document.title.substr(0, idx);
                    }
                });

                // manage boss info manually when a node or link is deleted from the diagram
                myDiagram.addDiagramListener("SelectionDeleting", function(e) {
                    var part = e.subject.first(); // e.subject is the myDiagram.selection collection,
                                                  // so we'll get the first since we know we only have one selection
                    myDiagram.startTransaction("clear boss");
                    if (part instanceof go.Node) {
                        var it = part.findTreeChildrenNodes(); // find all child nodes
                        while(it.next()) { // now iterate through them and clear out the boss information
                            var child = it.value;
                            var bossText = child.findObject("boss"); // since the boss TextBlock is named, we can access it by name
                            if (bossText === null) return;
                            bossText.text = "";
                        }
                    } else if (part instanceof go.Link) {
                        var child = part.toNode;
                        var bossText = child.findObject("boss"); // since the boss TextBlock is named, we can access it by name
                        if (bossText === null) return;
                        bossText.text = "";
                    }
                    myDiagram.commitTransaction("clear boss");
                });

                var levelColors = ["rgba(255,255,255,1)", "rgba(255,255,255,1)", "rgba(255,255,255,1)", "rgba(255,255,255,1)",
                    "rgba(255,255,255,1)", "rgba(255,255,255,1)", "rgba(255,255,255,1)", "rgba(255,255,255,1)"];

                // override TreeLayout.commitNodes to also modify the background brush based on the tree depth level
                myDiagram.layout.commitNodes = function() {
                    go.TreeLayout.prototype.commitNodes.call(myDiagram.layout);  // do the standard behavior
                    // then go through all of the vertexes and set their corresponding node's Shape.fill
                    // to a brush dependent on the TreeVertex.level value
                    myDiagram.layout.network.vertexes.each(function(v) {
                        if (v.node) {
                            var level = v.level % (levelColors.length);
                            var color = levelColors[level];
                            var shape = v.node.findObject("SHAPE");
                            if (shape) shape.fill = goMake(go.Brush, "Linear", { 0: color, 1: go.Brush.lightenBy(color, 0.05), start: go.Spot.Left, end: go.Spot.Right });
                        }
                    });
                };

                // This function is used to find a suitable ID when modifying/creating nodes.
                // We used the counter combined with findNodeDataForKey to ensure uniqueness.
                function getNextKey() {
                    var key = nodeIdCounter;
                    while (myDiagram.model.findNodeDataForKey(key) !== null) {
                        key = nodeIdCounter--;
                    }
                    return key;
                }

                var nodeIdCounter = -1; // use a sequence to guarantee key uniqueness as we add/remove/modify nodes

                // when a node is double-clicked, add a child to it
                function nodeDoubleClick(e, obj) {
                    // var clicked = obj.part;
                    // if (clicked !== null) {
                    //     var thisemp = clicked.data;
                    //     myDiagram.startTransaction("add employee");
                    //     var newemp = { key: getNextKey(), name: "(new person)", title: "", parent: thisemp.key };
                    //     myDiagram.model.addNodeData(newemp);
                    //     myDiagram.commitTransaction("add employee");
                    // }
                }

                // this is used to determine feedback during drags
                function mayWorkFor(node1, node2) {
                    if (!(node1 instanceof go.Node)) return false;  // must be a Node
                    if (node1 === node2) return false;  // cannot work for yourself
                    if (node2.isInTreeOf(node1)) return false;  // cannot work for someone who works for you
                    return true;
                }

                // This function provides a common style for most of the TextBlocks.
                // Some of these values may be overridden in a particular TextBlock.
                function textStyle() {
                    return { font: "9pt  Segoe UI,sans-serif", stroke: "black" };
                }

                // This converter is used by the Picture.
                function findHeadShot(key) {
                    if(key){
                        return basepath+"/res/modules/groundServiceScheduling/duty/images/HS" + parseInt(Math.random()*16)+ ".png"
                    }
                    //if (key < 0 || key > 16) return "./modules/groundServiceScheduling/duty/images/HSnopic.png"; // There are only 16 images on the server

                }

                // define the Node template
                myDiagram.nodeTemplate =
                    goMake(go.Node, "Auto",
                        {
                            doubleClick: nodeDoubleClick,
                            selectable:false
                         },
                        { // handle dragging a Node onto a Node to (maybe) change the reporting relationship
                            mouseDragEnter: function (e, node, prev) {
                                var diagram = node.diagram;
                                var selnode = diagram.selection.first();
                                if (!mayWorkFor(selnode, node)) return;
                                var shape = node.findObject("SHAPE");
                                if (shape) {
                                    shape._prevFill = shape.fill;  // remember the original brush
                                    shape.fill = "darkred";
                                }
                            },
                            mouseDragLeave: function (e, node, next) {
                                var shape = node.findObject("SHAPE");
                                if (shape && shape._prevFill) {
                                    shape.fill = shape._prevFill;  // restore the original brush
                                }
                            },
                            mouseDrop: function (e, node) {
                                // var diagram = node.diagram;
                                // var selnode = diagram.selection.first();  // assume just one Node in selection
                                // if (mayWorkFor(selnode, node)) {
                                //     // find any existing link into the selected node
                                //     var link = selnode.findTreeParentLink();
                                //     if (link !== null) {  // reconnect any existing link
                                //         link.fromNode = node;
                                //     } else {  // else create a new link
                                //         diagram.toolManager.linkingTool.insertLink(node, node.port, selnode, selnode.port);
                                //     }
                                // }
                            }
                        },
                        // for sorting, have the Node.text be the data.name
                        new go.Binding("text", "name"),
                        // bind the Part.layerName to control the Node's layer depending on whether it isSelected
                        new go.Binding("layerName", "isSelected", function(sel) { return sel ? "Foreground" : ""; }).ofObject(),
                        // define the node's outer shape
                        goMake(go.Shape, "Rectangle",
                            {
                                name: "SHAPE", fill: "white", stroke: null,
                                portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer"
                            }),
                        goMake(go.Panel, "Horizontal",
                            goMake(go.Picture,
                                {
                                    name: "Picture",
                                    desiredSize: new go.Size(39, 50),
                                    margin: new go.Margin(6, 8, 6, 10),
                                },
                                new go.Binding("source", "name", findHeadShot)),
                            // define the panel where the text will appear
                            goMake(go.Panel, "Table",
                                {
                                    maxSize: new go.Size(150, 999),
                                    margin: new go.Margin(6, 10, 0, 3),
                                    defaultAlignment: go.Spot.Left
                                },
                                goMake(go.RowColumnDefinition, { column: 2, width: 4 }),
                                goMake(go.TextBlock, textStyle(),  // the name
                                    {
                                        row: 0, column: 0, columnSpan: 5,
                                        font: "12pt Segoe UI,sans-serif",
                                        editable: true, isMultiline: false,
                                        minSize: new go.Size(10, 16)
                                    },
                                    new go.Binding("text", "name").makeTwoWay()),
                                goMake(go.TextBlock, "部门: ", textStyle(),
                                    { row: 1, column: 0 }),
                                goMake(go.TextBlock, textStyle(),
                                    {
                                        row: 1, column: 1, columnSpan: 4,
                                        editable: true, isMultiline: false,
                                        minSize: new go.Size(10, 14),
                                        margin: new go.Margin(0, 0, 0, 3)
                                    },
                                    new go.Binding("text", "title").makeTwoWay()),
                                // goMake(go.TextBlock, textStyle(),
                                //     { row: 2, column: 0 },
                                //     new go.Binding("text", "key", function(v) {return "ID: " + v;})),
                                // goMake(go.TextBlock, textStyle(),
                                //     { name: "boss", row: 2, column: 3, }, // we include a name so we can access this TextBlock when deleting Nodes/Links
                                //     new go.Binding("text", "parent", function(v) {return "Boss: " + v;})),
                                goMake(go.TextBlock, textStyle(),  // the comments
                                    {
                                        row: 3, column: 0, columnSpan: 5,
                                        font: "italic 9pt sans-serif",
                                        wrap: go.TextBlock.WrapFit,
                                        editable: true,  // by default newlines are allowed
                                        minSize: new go.Size(10, 14)
                                    },
                                    new go.Binding("text", "comments").makeTwoWay())
                            )  // end Table Panel
                        ) // end Horizontal Panel
                    );  // end Node

                // the context menu allows users to make a position vacant,
                // remove a role and reassign the subtree, or remove a department
                // myDiagram.nodeTemplate.contextMenu =
                //     goMake(go.Adornment, "Vertical",
                //         goMake("ContextMenuButton",
                //             goMake(go.TextBlock, "Vacate Position"),
                //             {
                //                 click: function(e, obj) {
                //                     var node = obj.part.adornedPart;
                //                     if (node !== null) {
                //                         var thisemp = node.data;
                //                         myDiagram.startTransaction("vacate");
                //                         // update the key, name, and comments
                //                         myDiagram.model.setKeyForNodeData(thisemp, getNextKey());
                //                         myDiagram.model.setDataProperty(thisemp, "name", "(Vacant)");
                //                         myDiagram.model.setDataProperty(thisemp, "comments", "");
                //                         myDiagram.commitTransaction("vacate");
                //                     }
                //                 }
                //             }
                //         ),
                //         goMake("ContextMenuButton",
                //             goMake(go.TextBlock, "Remove Role"),
                //             {
                //                 click: function(e, obj) {
                //                     // reparent the subtree to this node's boss, then remove the node
                //                     var node = obj.part.adornedPart;
                //                     if (node !== null) {
                //                         myDiagram.startTransaction("reparent remove");
                //                         var chl = node.findTreeChildrenNodes();
                //                         // iterate through the children and set their parent key to our selected node's parent key
                //                         while(chl.next()) {
                //                             var emp = chl.value;
                //                             myDiagram.model.setParentKeyForNodeData(emp.data, node.findTreeParentNode().data.key);
                //                         }
                //                         // and now remove the selected node itself
                //                         myDiagram.model.removeNodeData(node.data);
                //                         myDiagram.commitTransaction("reparent remove");
                //                     }
                //                 }
                //             }
                //         ),
                //         goMake("ContextMenuButton",
                //             goMake(go.TextBlock, "Remove Department"),
                //             {
                //                 click: function(e, obj) {
                //                     // remove the whole subtree, including the node itself
                //                     var node = obj.part.adornedPart;
                //                     if (node !== null) {
                //                         myDiagram.startTransaction("remove dept");
                //                         myDiagram.removeParts(node.findTreeParts());
                //                         myDiagram.commitTransaction("remove dept");
                //                     }
                //                 }
                //             }
                //         )
                //     );

                // define the Link template
                myDiagram.linkTemplate =
                    goMake(go.Link, go.Link.Orthogonal,
                        { corner: 5, relinkableFrom: true, relinkableTo: true },
                        {selectable:false},
                        goMake(go.Shape, { strokeWidth: 1, stroke: "#000" }));  // the link shape

                // read in the JSON-format data from the "mySavedModel" element
                // load();
                // myDiagram.model=go.Model.fromJson({ "class": "go.TreeModel",
                //     "nodeDataArray": [
                //         {"key":1, "name":"张三", "title":"CEO"},
                //         {"key":2, "name":"李四", "title":"VP Marketing/Sales", "parent":1},
                //         {"key":3, "name":"王五", "title":"Sales", "parent":2},
                //         {"key":4, "name":"马六", "title":"VP Engineering", "parent":1},
                //         {"key":5, "name":"冯奇", "title":"Manufacturing", "parent":4},
                //         {"key":6, "name":"李若水", "title":"Marketing", "parent":2},
                //         {"key":7, "name":"张其清", "title":"Sales Rep", "parent":3},
                //         {"key":8, "name":"周峰寺", "title":"Project Mgr", "parent":5},
                //         {"key":9, "name":"吴九七", "title":"Events Mgr", "parent":6},
                //         {"key":10, "name":"郑成风", "title":"Engineering", "parent":4},
                //         {"key":11, "name":"王春雷", "title":"Process", "parent":5},
                //         {"key":12, "name":"冯紫枫", "title":"Software", "parent":10},
                //         {"key":13, "name":"陈红", "title":"Testing", "parent":10},
                //         {"key":14, "name":"储征", "title":"Hardware", "parent":10},
                //         {"key":15, "name":"魏子翔", "title":"Quality", "parent":5},
                //         {"key":16, "name":"蒋中正", "title":"Sales Rep", "parent":3}
                //     ]
                // });



                // support editing the properties of the selected person in HTML
                if (window.Inspector) myInspector = new Inspector("myInspector", myDiagram,
                    {
                        properties: {
                            "key": { readOnly: true },
                            "comments": {}
                        }
                    });
            }
        },
        created:function (){
            var $this = this;
            setTimeout(function () {
                $this.draw();
            });


        },
        activated:function () {
            messages.$emit('change','日值班表');
        }
    })
    module.exports = VueComponent;
    return VueComponent;
});

